package com.zheng.umbrella.web.controller.manage;

import com.zheng.common.base.BaseController;
import com.zheng.common.constant.Constant;
import com.zheng.umbrella.common.constant.UmbrellaConstant;
import com.zheng.umbrella.common.constant.UmbrellaResult;
import com.zheng.umbrella.common.constant.UmbrellaResultConstant;
import com.zheng.umbrella.dao.model.UmbrellaRuleRent;
import com.zheng.umbrella.dao.model.UmbrellaRuleRentExample;
import com.zheng.umbrella.rpc.api.UmbrellaRuleRentService;
import com.zheng.upms.dao.model.UpmsDict;
import com.zheng.upms.dao.model.UpmsUser;
import com.zheng.upms.rpc.api.UpmsApiService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/9/16.
 */
@Controller
@Api(value = "租金规则管理", description = "租金规则管理")
@RequestMapping("/manage/rent")
public class UmbrellaRuleRentController extends BaseController {


    private static final Logger LOGGER = LoggerFactory.getLogger(UmbrellaRuleRentController.class);

    @Autowired
    private UmbrellaRuleRentService umbrellaRuleRentService;

    @Autowired
    private UpmsApiService upmsApiService;

    @ApiOperation(value = "租金规则首页")
    @RequiresPermissions("umbrella:rent:read")
    @RequestMapping(value = "/index", method = {RequestMethod.GET, RequestMethod.POST})
    public String index() {
        return "/manage/rent/index.jsp";
    }

    @ApiOperation(value = "租金规则列表")
    @RequiresPermissions("umbrella:rent:read")
    @RequestMapping(value = "/list", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Object list(
            @RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            @RequestParam(required = false, defaultValue = "", value = "search") String search,
            @RequestParam(required = false, value = "sort") String sort,
            @RequestParam(required = false, value = "order") String order) {
        UmbrellaRuleRentExample umbrellaRuleRentExample = new UmbrellaRuleRentExample();
        UmbrellaRuleRentExample.Criteria criteria = umbrellaRuleRentExample.createCriteria();
        criteria.andFlagEqualTo(Constant.CONST_FLAG_NORMAL);
        if (!StringUtils.isBlank(sort) && !StringUtils.isBlank(order)) {
            umbrellaRuleRentExample.setOrderByClause(sort + " " + order);
        }
        if (StringUtils.isNotBlank(search)) {
//            umbrellaRuleRentExample.or()
            criteria.andNameLike("%" + search + "%");
        }
        Integer agentId = upmsApiService.selectUserAgentIdByUsername(getLoginUserName());
        if(agentId != null){
            criteria.andAgentIdEqualTo(agentId);
        }else{
            criteria.andAgentIdEqualTo(-1);
        }
        List<UmbrellaRuleRent> rows = umbrellaRuleRentService.selectByExampleForOffsetPage(umbrellaRuleRentExample, offset, limit);
        long total = umbrellaRuleRentService.countByExample(umbrellaRuleRentExample);
        Map<String, Object> result = new HashMap<>();
        result.put("rows", rows);
        result.put("total", total);
        return result;
    }

    @ApiOperation(value = "新增租金规则")
    @RequiresPermissions("umbrella:rent:create")
    @RequestMapping(value = "/create", method = {RequestMethod.GET})
    public String create(ModelMap modelMap) {

        List<UpmsDict> umpsDictList = upmsApiService.selectUpmsDictByCode(UmbrellaConstant.UM_RULE_TYPE);
        modelMap.put("umpsDictList", umpsDictList);
        return "/manage/rent/create.jsp";
    }

    @ApiOperation(value = "新增租金规则")
    @RequiresPermissions("umbrella:rent:create")
    @ResponseBody
    @RequestMapping(value = "/create", method = {RequestMethod.POST})
    public Object create(UmbrellaRuleRent umbrellaRuleRent) {
//        ComplexResult result = FluentValidator.checkAll()
//                .on(upmsAgent.getName(), new LengthValidator(1, 120, "名称"))
//                .doValidate()
//                .result(ResultCollectors.toComplex());
//        if (!result.isSuccess()) {
//            return new UpmsResult(UpmsResultConstant.INVALID_LENGTH, result.getErrors());
//        }
        long time = System.currentTimeMillis();
        UpmsUser user = upmsApiService.selectUpmsUserByUsername(getLoginUserName());
        umbrellaRuleRent.setCtime(time);
        umbrellaRuleRent.setCuser(user.getUsername());
        umbrellaRuleRent.setAgentId(user.getAgentId());
        int count = umbrellaRuleRentService.insertSelective(umbrellaRuleRent);
        return new UmbrellaResult(UmbrellaResultConstant.SUCCESS, count);
    }

    @ApiOperation(value = "删除租金规则")
    @RequiresPermissions("umbrella:rent:delete")
    @RequestMapping(value = "/delete/{ids}",method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Object delete(@PathVariable("ids") String ids) {
//        int count = umbrellaRuleRentService.deleteByPrimaryKeys(ids);
        int count = umbrellaRuleRentService.deleteLogicByPrimaryKeys(ids,UmbrellaRuleRent.class,null);
        return new UmbrellaResult(UmbrellaResultConstant.SUCCESS, count);
    }

    @ApiOperation(value = "修改租金规则")
    @RequiresPermissions("umbrella:rent:update")
    @RequestMapping(value = "/update/{id}", method = {RequestMethod.GET})
    public String update(@PathVariable("id") int id, ModelMap modelMap) {
        List<UpmsDict> umpsDictList = upmsApiService.selectUpmsDictByCode(UmbrellaConstant.UM_RULE_TYPE);
        modelMap.put("umpsDictList", umpsDictList);
        UmbrellaRuleRent umbrellaRuleRent = umbrellaRuleRentService.selectByPrimaryKey(id);
        modelMap.put("umbrellaRuleRent", umbrellaRuleRent);
        return "/manage/rent/update.jsp";
    }

    @ApiOperation(value = "修改租金规则")
    @RequiresPermissions("umbrella:rent:update")
    @RequestMapping(value = "/update/{id}", method = {RequestMethod.POST})
    @ResponseBody
    public Object update(@PathVariable("id") int id, UmbrellaRuleRent umbrellaRuleRent) {
//        ComplexResult result = FluentValidator.checkAll()
//                .on(upmsAgent.getName(), new LengthValidator(1, 20, "名称"))
//                .doValidate()
//                .result(ResultCollectors.toComplex());
//        if (!result.isSuccess()) {
//            return new UpmsResult(UpmsResultConstant.INVALID_LENGTH, result.getErrors());
//        }
        umbrellaRuleRent.setId(id);
        int count = umbrellaRuleRentService.updateByPrimaryKeySelective(umbrellaRuleRent);
        return new UmbrellaResult(UmbrellaResultConstant.SUCCESS, count);
    }


}
